package com.hex.liyue.usermodule.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hex.liyue.R;
import com.hex.liyue.commonmodule.activity.BaseActivity;
import com.hex.liyue.commonmodule.activity.WebActivity;
import com.hex.liyue.commonmodule.manager.UserManager;
import com.hex.liyue.commonmodule.utils.DateUtils;
import com.hex.liyue.commonmodule.utils.SPUtil;
import com.hex.liyue.commonmodule.utils.SexUtils;
import com.hex.liyue.commonmodule.utils.StatusBarCompatUtils;
import com.hex.liyue.commonmodule.view.recycleview.YRecycleview;
import com.hex.liyue.usermodule.adapter.AdapterFriends;
import com.hex.liyue.usermodule.bean.MyFriendsResultBean;
import com.hex.liyue.usermodule.bean.MyUpLinesResultBean;
import com.hex.liyue.usermodule.presenter.FriendsPresenter;
import com.hex.liyue.usermodule.presenter.IFriendsView;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by WJ on 2016/6/15.
 * <p>
 * 我的伙伴界面和逻辑的实现
 */
public class FriendsActivity extends BaseActivity implements IFriendsView, View.OnClickListener {
    @Bind(R.id.iv_toobar_back)
    ImageView ivToobarBack;
    @Bind(R.id.tv_toobar_title)
    TextView tvToobarTitle;
    @Bind(R.id.tv_toobar_right)
    TextView tvToobarRight;
    @Bind(R.id.rv_friends_list)
    YRecycleview rvFriendsList;

    private TextView tvSelfInviteCode, tvInviteCode, tvMyFriendsSure, tvMyFriendsMore;
    private EditText etInviteCode;
    private LinearLayout llInviteBox;
    private FriendsPresenter friendsPresenter;
    private AdapterFriends adapterFriends;
    private TextView tv_me_friends_this_why;

    @Override
    protected void bindPresenter() {
        friendsPresenter = new FriendsPresenter(this);
    }

    @Override
    protected void initData(Bundle extras) {
        View headView = View.inflate(this, R.layout.head_my_friends, null);
        tvSelfInviteCode = (TextView) headView.findViewById(R.id.tv_self_invite_code);
        etInviteCode = (EditText) headView.findViewById(R.id.et_invite_code);
        tvMyFriendsSure = (TextView) headView.findViewById(R.id.tv_myfriends_sure);
        tvMyFriendsSure.setOnClickListener(this);
        tvMyFriendsMore = (TextView) headView.findViewById(R.id.tv_myfriends_more);
        tvMyFriendsMore.setOnClickListener(this);
        tvInviteCode = (TextView) headView.findViewById(R.id.tv_invite_code);
        llInviteBox = (LinearLayout) headView.findViewById(R.id.ll_invite_box);
        tv_me_friends_this_why = (TextView) headView.findViewById(R.id.tv_me_friends_this_why);
        rvFriendsList.addHeadView(headView);
        rvFriendsList.setReFreshEnabled(false);
        rvFriendsList.setLoadMoreEnabled(false);
        tv_me_friends_this_why.setOnClickListener(this);
        adapterFriends = new AdapterFriends();
        rvFriendsList.setLayoutManager(new LinearLayoutManager(this));
        rvFriendsList.setAdapter(adapterFriends);
        tvSelfInviteCode.setText(UserManager.getSelfInviteCode());//显示我的邀请码
        friendsPresenter.myFrendis();
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_friends;
    }

    @Override
    protected void initToolbar() {
        StatusBarCompatUtils.compat(this);
        ivToobarBack.setVisibility(View.VISIBLE);
        tvToobarTitle.setText("我的公司");
        if ("2".equals(UserManager.getUserType())) {
            tvToobarRight.setVisibility(View.VISIBLE);
            tvToobarRight.setText("发送邀请码");
        }else{
            tvToobarRight.setVisibility(View.GONE);
        }
    }


    @OnClick({R.id.iv_toobar_back, R.id.tv_toobar_right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_toobar_back:
                onBackPressed();
                break;
            case R.id.tv_myfriends_sure:
                friendsPresenter.offLinesMyFrendis();
                break;
            case R.id.tv_myfriends_more:
                startMoreFriendsActivity();
                break;
            case R.id.tv_toobar_right:
                startRecommendActivity();
                break;
            case R.id.tv_me_friends_this_why:
                startWebActivity(null);
                break;
        }
    }

    @Override
    public void startMoreFriendsActivity() {
        Intent intent = new Intent(FriendsActivity.this, MoreFriendsActivity.class);
        startActivity(intent);
    }

    @Override
    public void setAdapter(MyUpLinesResultBean myUpLinesResultBean) {
        friendsPresenter.myFrendis();
        UserManager.setInviteCode(myUpLinesResultBean.data.invite_code);
        SPUtil.putString(this, "invite_code", myUpLinesResultBean.data.invite_code);
    }

    @Override
    public String getInviteCode() {
        return etInviteCode.getText().toString().trim();
    }

    @Override
    public void startRecommendActivity() {
        Intent intent = new Intent(FriendsActivity.this, RecommendActivity.class);
        startActivity(intent);
    }

    @Override
    public void startWebActivity(String url) {
        Intent intent = new Intent(FriendsActivity.this, WebActivity.class);
        intent.putExtra("url", "file:///android_asset/rule.html");
        intent.putExtra("title", "规则");
        startActivity(intent);
    }

    @Override
    public void setAdapter(MyFriendsResultBean myFriendsResultBean) {
        tvInviteCode.setVisibility(View.GONE);
        llInviteBox.setVisibility(View.GONE);
        if("3".equals(UserManager.getUserType())){
            llInviteBox.setVisibility(View.GONE);
        }else{
            if (myFriendsResultBean.data.from_invitor == null) {
                llInviteBox.setVisibility(View.VISIBLE);
            } else {
                //有上线信息 本地更新上线信息

                long inviteTime = myFriendsResultBean.data.from_invitor.invite_time * 1000;
                tvInviteCode.setText("我于" + DateUtils.getFormatDate(inviteTime, "yyyy年MM月dd日") + "被" + myFriendsResultBean.data.from_invitor.nickname + "邀请为" + SexUtils.getXSex(myFriendsResultBean.data.from_invitor.sex) + "的小伙伴");
                tvInviteCode.setVisibility(View.VISIBLE);
            }
        }
        adapterFriends.setList(myFriendsResultBean.data.mates);
        adapterFriends.notifyDataSetChanged();
    }

    @Override
    public void showLod(String msg) {
        showLoad(FriendsActivity.this, msg);
    }

    @Override
    public void closeLod() {
        closeLoad();
    }

    @Override
    public void showT(String msg) {
        showToast(this, msg);
    }
}
